// pages/department-list/department-list.js
const app = getApp()
Page({
  data: {
    departmentList: [
      
    ],
    departmentNineList: [
      
    ],
    topHospitals: [
    
    ],
	showAll: false      // 是否显示全部科室
  },
onLoad(options) {
	const self = this
	wx.checkSession({
		success() { 
			app.util.getUserInfo(function (response) {
				console.log("==获取当前用户信息1==");
				console.log(response);
				self.setData({
					userInfo: response
				});
				console.log("==应用初始化：应用配置/应用中用户信息/当前城市信息==");
				
				setTimeout(function(){
					self.loadKeshiData(true)
					self.loadHostipalData(true)
				},500)
			});
		},
		fail() {
			app.util.getUserInfo(function (response) {
				console.log("==获取当前用户信息1==");
				console.log(response);
				self.setData({
					userInfo: response
				});
				console.log("==应用初始化：应用配置/应用中用户信息/当前城市信息==");
				
				setTimeout(function(){
					self.loadKeshiData(true)
					self.loadHostipalData(true)
				},500)
			});
		}
	});
    
  },
// 加载数据

  async loadHostipalData(init = false) {
	  var that = this
    try {
      // 模拟API请求
      app.util.request({
              url: 'Hospital/bdyllists',
              data: {
              },
              success(res) {
                var data = res.data.data;
      			var list = data.hospital
                that.setData({
                    topHospitals: list
                });
              }
      })
    } catch (error) {
      this.setData({ isLoading: false })
      wx.showToast({ title: '加载失败', icon: 'none' })
    }
  },
  async loadKeshiData(init = false) {
	  var that = this
    try {
      // 模拟API请求
      app.util.request({
              url: 'Keshi/keshilists',
              data: {
              },
              success(res) {
                var data = res.data.data;
      			var list = data.keshi
				console.log(list)
				console.log(data)
                that.setData({
                    departmentList: list,
					departmentNineList:list.slice(0,9)
                });
              }
      })
    } catch (error) {
      this.setData({ isLoading: false })
      wx.showToast({ title: '加载失败', icon: 'none' })
    }
  },
  // 切换展开收起状态
    toggleExpand: function() {
      this.setData({
        showAll: !this.data.showAll
      });
    },
  // 跳转医院列表
  navigateToHospitalList(e) {
    const departmentId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/vp_pz/pages/bdyl/list?departmentId=${departmentId}`
    })
  },

  // 跳转医院详情
  navigateToHospitalDetail(e) {
    const hospitalId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/vp_pz/pages/bdyl/hospital?id=${hospitalId}`
    })
  }
})